<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	
</style>
<div class="page-header">
	<h1>待处理的申请</h1>
</div>
<div class="col-xs-12 content-filter">
	<div class="widget-box">
		<div class="widget-header">
			<h4 class="widget-title">内容筛选</h4>
			<div class="widget-toolbar">
				<a href="#" data-action="collapse">
					<i class="ace-icon fa fa-chevron-up"></i>
				</a>
				<a href="#" data-action="close">
					<i class="ace-icon fa fa-times"></i>
				</a>
			</div>
		</div>
		<div class="widget-body">
			<div class="widget-main">
				<div class="form-inline" id="data-time">
					<div class="form-inline keyword-search-div">
						<label class="keyword-search-label">发起时间</label>
						<div class="keyword-search-box">
							<div class="div-search">
								<input id="data-search-time" style="float:left;height:30px;width:130px;border:1px solid #669FC7;padding:0 30px 0 14px;border-radius:10px !important;-moz-border-radius:10px" type="text" name="title" placeholder="选择日期..." autocomplete="off">
								<span id="empty-search" style="position:relative;right:20px;cursor:pointer;display:none;">×</span>
							</div>
						</div>
					</div>
					<div class="form-inline keyword-search-div" id="div-workflow">
						<label class="keyword-search-label">工作流&nbsp;&nbsp;&nbsp;&nbsp;</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
							<?php if(isset($data['workflows'])): ?>
								<?php foreach($data['workflows'] as $workflow): ?>
									<a href="javascript:;" class="keyword-search-keyword" data-id="<?= $workflow['id']?>"><?=$workflow['workflow_title'] ?></a>
								<?php endforeach; ?>
							<?php endif ?>
						</div>
					</div>
					<div class="form-inline keyword-search-div" id="div-state">
						<label class="keyword-search-label">审批状态</label>
						<div class="keyword-search-box">
							<a href="javascript:;" class="keyword-search-keyword active" data-id="0">全部</a>
							<?php if(isset($data['states'])): ?>
								<?php foreach($data['states'] as $state): ?>
									<a href="javascript:;" class="keyword-search-keyword" data-id="<?= $state['value']?>"><?=$state['title'] ?></a>
								<?php endforeach; ?>
							<?php endif ?>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="col-xs-12">
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>申请编号</th>
                <th>工作流名称</th>
                <th>申请人</th>
				<th>审批状态</th>
				<th>申请内容</th>
				<th>发起时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var table = "";
	$(function(){
		// 通过申请
		$("body").on("click", ".accept-application", function(){
			var id = $(this).data("id");
			layer.confirm('确定通过这个申请吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('workflow/accept_application')?>";
				var postData = {"id": id};
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
		// 拒绝申请
		$("body").on("click", ".refuse-application", function(){
			var id = $(this).data("id");
			layer.confirm('确定拒绝这个申请吗？', function(){
				// 提交数据
				var postUrl = "<?=admin_url('workflow/refuse_application')?>";
				var postData = {"id": id};
				ajax_post(postUrl, postData, function(res){
					table.draw(false);
				})
			});
		})
		
		// 工作流列表
		table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('workflow/get_handling_application_list')?>',
			"columns": [
				{"data": function(ret){
	            	return ret.id;
				}},
                {"data": "workflow_title"},
                {"data": function(ret){
	            	return ret.username;
				}},
	            {"data": function(ret){
					var applicationState = '<span class="label label-info">待审批</span>';
					if(ret.state === '<?= APPLICATION_ONGOING ?>') applicationState = '<span class="label label-warning">审批中</span>';
	            	if(ret.state === '<?= APPLICATION_ACCEPTED ?>') applicationState = '<span class="label label-success">已通过</span>';
	            	if(ret.state == '<?= APPLICATION_REJECTED ?>') applicationState = '<span class="label label-danger">已拒绝</span>';

	            	return applicationState;
	            }},
	            {"data": function(ret){
	            	return '<a href="<?=admin_url('workflow/check_application_by_handling_user')?>/'+ ret.id + '"><i class="fa fa-table"></i> 查看申请内容</i></a>';
				}},
				{"data": function(ret){
	            	return ret.createtime;
	            }},
	            {"data": function(ret){
	            	var action = '';
	            	action += '<a data-id="'+ ret.id +'" class="accept-application text-success" href="javascript:;"><i class="fa fa-check"></i> 通过 </a> &nbsp;&nbsp;&nbsp;&nbsp;';
	            	action += '<a data-id="'+ ret.id +'" class="refuse-application text-danger" href="javascript:;"><i class="fa fa-times"></i> 拒绝 </a>';
	                return action;
	            }},
			]
		});
		// 点击关键字
		$("body").on("click", ".keyword-search-keyword", function(){
			var id = $(this).data("id");
			// 高亮当前点击对象
			$(this).parent().find(".keyword-search-keyword").removeClass("active");
			$(this).addClass("active");
			// 筛选内容
			var createtime = $("#data-search-time").val();
			var workflowId = $("#div-workflow .active").data('id');
			var state = $("#div-state .active").data('id');
			table.setAjaxData({
				"workflowId": workflowId,
				"state": state,
				"createtime": createtime
			});
		})
		// 清空搜索框
		$("#data-search-time").keyup(function() {
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=title]").val('');
					$(this).hide();
					// 重置搜索结果
					var workflowId = $("#div-workflow .active").data('id');
					var state = $("#div-state .active").data('id');
					table.setAjaxData({
						"workflowId": workflowId,
						"state": state,
					});
				})
			} else {
				$("#empty-search").hide();
			}
		}); 
		// 筛选日期	
		$("#btn-search").click(function() {
			var title = $("[name=title]").val();
			var classId = $(".div-classes > .active").data("id");
			if(title != '') {
				table.setAjaxData({
					"class_id": classId,
					'title': title
				})
			} else {
				table.setAjaxData({
					"class_id": classId,
				})
			}
		})
		// 日期选择控件
		$("#data-search-time").datepicker({
			autoclose: true,
			todayHighlight: true,
			endDate: "<?= date('Y-m-d') ?>"
		}).on('changeDate', function(){
			// 显示清空日期按钮
			if($(this).val().length > 0) {
				$("#empty-search").show();
				$("#empty-search").click(function(){
					$("[name=title]").val('');
					$(this).hide();
					// 重置搜索结果
					var workflowId = $("#div-workflow .active").data('id');
					var state = $("#div-state .active").data('id');
					table.setAjaxData({
						"workflowId": workflowId,
						"state": state,
					});
				})
			} else {
				$("#empty-search").hide();
			}

			// 筛选内容
			var createtime = $(this).val();
			var workflowId = $("#div-workflow .active").data('id');
			var state = $("#div-state .active").data('id');
			table.setAjaxData({
				"workflowId": workflowId,
				"state": state,
				"createtime": createtime
			});
		});
		// 日期选择控件
		$(".time-input").datetimepicker({
			format: 'YYYY-MM-DD'
		})
	})
</script>